

    const mysql = require('../lib/mysql')
    const {user, role} = require('../model/index')

    module.exports = {

        checklogin: async (username, password) => {
            let query = `select password from users where username = '${username}'`
            return mysql.allSqlAction(query).then(res => {
                if(res[0].password===password) return true
                else return false
            })
        },

        sequelizelogin: async (username, password) => {
            try {
                let data = await user.findOne({
                    where: {
                        username: username
                    }
                })
                if(data && data.dataValues.password === password) return {
                    login: true,
                    data: data.dataValues
                };
                return false;
            } catch(error) {
                console.log("查找用户错误：" + error)
            }
            
        },

        returnUserInfo: async () => {
            try {
                let userdata = await user.findAll({
                    attributes: ["id", "username", "phone", "roleid"],
                })
                let userinfo = []
                for(let i=0;i<userdata.length;i++) {
                    userinfo.push(userdata[i].dataValues)
                }
                return userinfo
            } catch(error) {
                console.log("查询错误" + error)
            }
        },

        returnRoleInfo: async () => {
            try {
                let roledata = await role.findAll({
                    attributes: ["roleid", "rolename"]
                })
                let roleinfo = []
                for(let i=0;i<roledata.length;i++) {
                    roleinfo.push(roledata[i].dataValues)
                }
                return roleinfo
            } catch(error) {
                console.log("查询错误" + error)
            }
        },

        searchUserData: async (query) => {
            try {
                let result = await user.findAll({
                    where: query,
                    attributes: ["id", "username", "phone", "roleid"],
                })
                return result
            } catch(error) {
                console.log("查询出错" + error)
                return null
            }
        },

        addUser: async (userinfo) => {
            try {
                await user.create({
                    username: userinfo.username,
                    password: userinfo.pass,
                    phone: userinfo.phone,
                    salt: '',
                    roleid: userinfo.roleid
                })
            } catch(error) {
                console.log("查询出错" + error)
                return false
            }
            return true
        },

        deleteUser: async (username) => {
            try {
                await user.destroy({
                    where: {
                        username: username
                    }
                })
            } catch(error) {
                console.log("查询出错" + error)
                return false
            }
            return true
        }
    }